﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI"/>
/// <reference path="../../Manager/Admin/Admin.asmx" />
Type.registerNamespace("motobankcorp");
motobankcorp.Bridge_Note = function(element) {
  motobankcorp.Bridge_Note.initializeBase(this, [element]);
  this._t_Note = null;
  this._initEvents(["loaded", "inserted"]);
  var props = { _selNoteTypesID: '', _selNoteActionsID: '', _selStatusesID: '', _loadingPanelID: '', _loanID: 0 };
  this._initProps(props);
}
motobankcorp.Bridge_Note.prototype = {
  // Methods
  getNewNoteInfo: function(loanID) {
    Dimok.FindWSMethod("Note_NewNoteInfo",WSA)(loanID, Function.createDelegate(this, this.fillNewNote));
    this._showLoadPanel(this);
    //if (loanID) $J(this.get_element()).stop(true).slideDown();
    //else $J(this.get_element()).stop(true).slideUp();
  },
  validate: function() {
    if (!parseInt(this.get_selNoteTypes().value)) return alert("Select Note Type first.");
    if (!parseInt(this.get_selNoteActions().value)) return alert("Select Action Code first.");
    return true;
  },
  show: function(loanID, sync) {
    if (!sync) {
      clearTimeout(arguments.callee.procid);
      return arguments.callee.procid = setTimeout(Function.createDelegate(this, function() { this.show(loanID, true); }), 300);
    }
    //if (!loanID) $J(this.get_element()).slideUp(); // else $J(this.get_element()).slideDown();
  },
  fillNewNote: function(result) {
    this._hideLoadPanel(this);
    result.NoteTypeID = this.get_selNoteTypes().value || 0;
    var xml = Dimok$MakeXmlFromJson(result);
    if (!xmlNewNote.loadXML(xml.xml)) alert("Error in xml:\n\n" + xml);
    xml = null; delete xml;
    this._t_Note = result;
    this.show(result.LoanID);
  },

  // Handlers
  _onchange_ActionCode: function() {
  Dimok.FindWSMethod("Note_StatusesByActionCode", WSA)(this.get_selNoteActions().value, Function.createDelegate(this, _cb), this.get_wsErrorDelegate());
    this._showLoadPanel(this);
    function _cb(result) {
      this._hideLoadPanel(this);
      Dimok.FillSelectFromJSON(this.get_selStatuses(), result);
    }
  },
  _onchange_NoteType: function(eventArg) {
  Dimok.FindWSMethod("Note_ActionCodesByNoteType", WSA)(eventArg.rawEvent.srcElement.value, Function.createDelegate(this, _cb), this.get_wsErrorDelegate());
    this._showLoadPanel(this);
    function _cb(result) {
      this._hideLoadPanel(this);
      Dimok.FillSelectFromJSON(this.get_selNoteActions(), result);
    }
  },
  _onchange_Status: function(eventArg) {
  Dimok.FindWSMethod("FetchNoteDateDue", WSA)(eventArg.rawEvent.srcElement.value, Function.createDelegate(this, _cb), this.get_wsErrorDelegate());
    this._showLoadPanel(this);
    function _cb(result) {
      this._hideLoadPanel(this);
      //if ($get("txtDateDue").changed) return;
      $get("txtDateDue").value = result;
    }
  },
  _onaddNote: function() {
    if (!this.validate()) return;
    var t_Note = {};
    for (var node = xmlNewNote.XMLDocument.documentElement.firstChild; node != null; node = node.nextSibling) {
      if (this._t_Note[node.tagName] === null && !node.text) continue; // delete t_Note[node.tagName];
      else t_Note[node.tagName] = node.text.replace(/<p>(.+)<\/>/gi, "$1");
    }
    t_Note.Note = $J("div[dataFld='Note']")[0].innerHTML.replace(/<p>/gi, "").replace(/<\/p>/gi, "\n");
    Dimok.FindWSMethod("Note_Add", WSA)(t_Note, Function.createDelegate(this, _cb), this.get_wsErrorDelegate());
    this._showLoadPanel(this);
    function _cb(result) {
      this._hideLoadPanel(this);
      this.fillNewNote(result);
      this.raise_inserted();
      var pe = this.findPageEventsGlobal();
      if (pe) pe.raise_noteInserted();
    }
  },
  _initShowExtPwdHover: function() {
    var element = this;
    //if ($J("#hvrExtPwdPanel").find("#txtExt")[0].value && $J("#hvrExtPwdPanel").find("#txtExt")[0]) return true;
    element.onmouseover = null;
    var hvrExtPwdID = "hvrExtPwd";
    var hvrExtPwdPanelID = "hvrExtPwdPanel";
    if ($find(hvrExtPwdID)) return;
    var hover = $create(AjaxControlToolkit.HoverMenuBehavior,
				{ "PopupPosition": AjaxControlToolkit.HoverMenuPopupPosition.Left,
				  "HoverDelay": 300,
				  "id": hvrExtPwdID,
				  "OffsetY": 0,
				  "popupElement": $J("#" + hvrExtPwdPanelID)[0]
				}, null, null, element);
    return false;
  },
  _onLoaded: function() {
    var pe = this.findPageEventsGlobal();
    if (!pe) return;
    pe.add_loanSelected(Function.createDelegate(this, function(sender, eventArgs) {
      $J(this.get_element()).stop().slideDown();
      this.getNewNoteInfo(eventArgs.get_keys().get_LoanID() || 0);
    }));
    pe.add_loanDeselected(Function.createDelegate(this, function() {
      this.getNewNoteInfo(0);
      $J(this.get_element()).stop().slideUp();
    }));
  },

  // Implementations
  initialize: function() {
    motobankcorp.Bridge_Note.callBaseMethod(this, 'initialize');
    this.addHandlers(this.get_selNoteTypes(), { 'change': this._onchange_NoteType }, this);
    this.addHandlers(this.get_selNoteActions(), { 'change': this._onchange_ActionCode }, this);
    this.addHandlers(this.get_selStatuses(), { 'change': this._onchange_Status }, this);
    this.addHandlers($get("btnAddNewNote"), { 'click': this._onaddNote }, this);
    var img = $J("#imgShowExtPwd", this.get_element())[0];
    this.addHandlers(img, { 'mouseover': this._initShowExtPwdHover }, img);
    //Sys.Application.add_load(function() {
    //$create(AjaxControlToolkit.CalendarBehavior, { popupPosition: AjaxControlToolkit.CalendarPosition.BottomLeft }, null, null, $get("txtDateDue"));
    $J("#txtDateDue").datepicker({ gotoCurrent: true, constrainInput: false });
    //});
    this._onLoaded();
    this.raise_loaded();
  },
  dispose: function() {
    //Add custom dispose actions here
    motobankcorp.Bridge_Note.callBaseMethod(this, 'dispose');
  }
}
motobankcorp.Bridge_Note.registerClass('motobankcorp.Bridge_Note', motobankcorp.UI.Control);
motobankcorp.Dialer = function() {
  this.dial = function(ext, pwd, notePhone, counter) {
    Dimok.FindWSMethod("Dial", WSA)(ext, pwd, notePhone, Function.createDelegate(this, cb), _wsError);
    function cb(result) {
      if (!result) return;
      if (counter > 10) return Dimok.FindWSMethod("OnHook", WSA)(ext, pwd, null, _wsError);
      if (result.toLowerCase() == "onhook")
        return setTimeout(function() { this.dial(ext, pwd, notePhone, (counter || 0) + 1) }, 1000);
      alert("Unknown result code [" + result + "].");
    }
    function _wsError(error) {
      alert(error.get_message());
    }
  };
  return this;
} ();
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

